<template>
  <div class="form-container" v-loading="loading">
    <el-form size="mini" ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item prop="msg_zone" label="短信物资区域">
        <el-input v-model="form.msg_zone" placeholder="请填写短信物资区域"></el-input>
        <div class="el-form-item__label" style="line-height: 22px">例如:河北石家庄</div>
      </el-form-item>
      <el-form-item prop="msg_title" label="短信物资标题">
        <el-input v-model="form.msg_title" placeholder="请填写短信物资标题"></el-input>
      </el-form-item>
      <el-form-item prop="online_url" label="外网链接">
        <el-input v-model="form.online_url" placeholder="请填写外网链接"></el-input>
      </el-form-item>
      <el-form-item prop="crm_address" label="crm链接地址">
        <el-input v-model="form.crm_address" placeholder="请填写crm链接地址"></el-input>
      </el-form-item>
      <el-form-item prop="number" label="每批发送数量">
        <el-input type="number" v-model="form.number" placeholder="请填写每批发送数量"></el-input>
      </el-form-item>
      <el-form-item>
        <div class="line">
          <el-button size="mini" @click="onCancel">取消</el-button>
          <el-button size="mini" type="primary" @click="onSubmit">提交</el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { apiCreatePlan } from '@/api/assetsDisposal'

export default {
  props: { searchParams: Object, total: Number },
  data() {
    var validaNumber = (rule, value, callback) => {
      if (value && value <= 0) {
        callback(new Error('发送数量必须大于0'))
      } else {
        callback()
      }
    }
    return {
      form: {
        msg_zone: '',
        msg_title: '',
        online_url: '',
        crm_address: '',
        number: '100',
      },
      rules: {
        msg_zone: [{ required: true, message: '请填写短信物资区域', trigger: 'blur' }],
        msg_title: [{ required: true, message: '请填写短信物资标题', trigger: 'blur' }],
        number: [
          { required: true, message: '请填写每批发送数量', trigger: 'blur' },
          { validator: validaNumber, trigger: 'blur' },
        ],
      },
      loading: false,
    }
  },
  computed: {},
  mounted() {},
  methods: {
    // 保存
    onSubmit() {
      this.$refs.form.validate((flag) => {
        if (flag) {
          this.loading = true
          const sendData = Object.assign(this.form, this.searchParams)
          console.log(sendData)
          const formData = new FormData()
          for (let key in sendData) {
            formData.set(key, sendData[key])
          }
          apiCreatePlan(formData)
            .then((res) => {
              this.loading = false
              if (res.code !== 200) {
                return
              }
              this.$message({
                message: '操作成功！',
                type: 'success',
              })
              setTimeout(() => {
                this.$emit('close')
              }, 1000)
            })
            .catch(() => {
              this.loading = false
            })
        }
      })
    },
    // 取消
    onCancel() {
      this.$emit('close')
    },
  },
}
</script>
<style lang="scss" scoped>
.form-container {
  padding: 0 24px;
}
.el-input {
  width: 100% !important;
}
.line {
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
</style>
<style>
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
  margin: 0;
}
input[type='number'] {
  -moz-appearance: textfield;
}
</style>
